package cn.zhizihua.controller;

import cn.zhizihua.pojo.Address;
import cn.zhizihua.query.AddressQuery;
import cn.zhizihua.service.IAddressService;
import cn.zhizihua.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController     //等于 @Controller+@ResponseBody响应参数为json
@RequestMapping("/address")   //配置请求路径  接收所有的请求方式   放在类上（模块路径）   方法上（方法路径）  方法后续具体路径@PostMapping....
public class AddressController {

    @Autowired
    IAddressService addressService;

    @PostMapping      //  请求方式 + 请求路径   post  /address
    public ResultBean save(@RequestBody Address address){    //@RequestBody（请求方式为JSON，必须有请求体） + 一般是前端使用axios发送请求时使用
        System.out.println("----AddressController-----save--------");
        System.out.println(address);
        if (address.getAddressId()==null){
            return addressService.add(address);
        }
        return addressService.update(address);
    }

    /*@PostMapping      //  请求方式 + 请求路径   post  /address
    public ResultBean add(@RequestBody Address address){
        System.out.println("----AddressController-----add--------");
        System.out.println(address);
        return addressService.add(address);
    }

    @PutMapping    //   put   /address
    public ResultBean update(@RequestBody Address address){
        System.out.println("-------AddressController--------update-------------");
        System.out.println(address);
        return addressService.update(address);
    }*/

    //  delete   /address/1003
    @DeleteMapping("/{id}")   // http://localhost:8080/address/1003
    public ResultBean del(@PathVariable("id") Integer id){
        System.out.println("-------AddressController--------del-------------");
        System.out.println(id);
        return addressService.delete(id);
    }

    //  get   /address/1004
    @GetMapping("/{id}")   // http://localhost:8080/address/1003
    public ResultBean getById(@PathVariable("id") Integer id){
        System.out.println("-------AddressController--------getById-------------");
        System.out.println(id);
        return addressService.getById(id);
    }

    @GetMapping("/all")    //  get  /address/all
    public ResultBean getAll(){
        System.out.println("-------AddressController-----getAll--------");
        return addressService.getAll();
    }

    @PostMapping("/page")   // post  /address/page
    public ResultBean getByPage(@RequestBody AddressQuery addressQuery){
        System.out.println("-------AddressController--------getByPage-------------");
        System.out.println(addressQuery);
        return addressService.getByPage(addressQuery);
    }

    @GetMapping("/user/{userId}")    //   get +  /address/user/1
    public ResultBean getByUserId(@PathVariable("userId") Integer userId){
        System.out.println("--------AddressController--------getByUserId---------");
        System.out.println(userId);
        return addressService.getByUserId(userId);
    }

    @PostMapping("/batch/{ids}")   // post + /food/batch
    public ResultBean batchDel(@PathVariable("ids") String ids){
        System.out.println("----------AddressController---------batchDel---------------");
        System.out.println(ids); //接收到的参数格式 ： 21,19
        String[] strIds = ids.split(",");  //字符串拆分  [21,19]
        System.out.println(Arrays.toString(strIds));
        List<Integer> numids = new ArrayList<>();  //创建一个Integer集合  service层需要的参数类型
        for (String strid: strIds) {
            numids.add(Integer.parseInt(strid));//将数组中的每一项转型为Integer再加入到集合中
        }
        System.out.println(numids);
        return addressService.batchDel(numids);
    }

}
